package com.hqyj.controller;

import com.hqyj.pojo.UserInfo;
import com.hqyj.service.UserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.UUID;

@Controller
public class IndexController {
    //访问index页面的路由
    @Autowired
    UserInfoService userInfoService;

    //获取图片上传的配置路劲
    @Value("${file.address}")
    String filrAdress;
    //用户访问的图片路径
    @Value("${file.staticPath}")
    String upload;


    @RequestMapping("/index")
    public String index(){
        return "index";
    }
    //访问修改密码页面UpdatePwd
    @RequestMapping("/updatePwd")
    public String updatePwd(){
        return "updatePwd";
    }
    //访问managermanager-index界面
    @RequestMapping("/managermanager-index")
    public String mmanager(){
        return "/managermanager-index";}

    //访问user-index界面
    @RequestMapping("/user-index")
    public String userind(){return "user-index";}

    //访问manager-index界面
    @RequestMapping("/manager-index")
    public  String manager(){
        return "manager-index";
    }


    //访问修改密码页面UpdatePwd
    @RequestMapping("/updateHeader")
    public String updateHeader(){
        return "updateHeader";
    }
    //处理修改密码的ajax请求
    @RequestMapping("/updatePwdAjax")
    @ResponseBody
    public HashMap<String,Object> updatePwdAjax(UserInfo user, HttpServletRequest request){
        HashMap<String,Object> map = new HashMap<>();
        String info = userInfoService.updatePwd(user,request);
        map.put("info",info);
        return map;
    }


    @RequestMapping("/getUser")
    @ResponseBody
    public HashMap<String,Object> getUser(HttpServletRequest request){
        HashMap<String,Object> map = new HashMap<>();

        HttpSession session = request.getSession();
        UserInfo user =(UserInfo)session.getAttribute("user");
        map.put("info",user.getUserName());
        return map;

    }
    @RequestMapping("/upload")
    @ResponseBody
    public HashMap<String,Object> upload(MultipartFile file){
        HashMap<String,Object> map = new HashMap<>();

        //上传文件
        try {
            //定义一个文件上传的目录
            String timeDir = "";
            //获取事件
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            timeDir = sdf.format(new Date());


            //定义上传文件的前缀
            String pre ="";
            //是为了保证文件上传后，存到服务器的文件名是唯一的
            pre = UUID.randomUUID()+"";
            //获取文件的后缀
            //获取上传图片的后缀
            String hou="";
            if(file!=null) {
                //。jpg
                String originalName = file.getOriginalFilename();
                hou = originalName.substring(originalName.lastIndexOf(".") + 1);
            }
            //定义文件上传的全路劲



            String filePath=filrAdress+"\\"+timeDir+"\\"+pre+"."+hou;


            //创建file对象
            File f= new  File(filePath);

            //判断目录是否存在，不存在创建目录
            if(!f.isDirectory()){
                //创建目录
                f.mkdirs();

            }
            file.transferTo(f);
            //设置上传提示信息
            map.put("code",0);
            //返回给前端 用户访问这个图片的路
            String path = upload+"\\"+timeDir+"\\"+pre+"."+hou;
            //
            map.put("src",path);
            return map;
        } catch (IOException e) {
            e.printStackTrace();
        }
        map.put("code",1);
        return map;

    }
    @RequestMapping("/saveHead")
    @ResponseBody
    public HashMap<String,Object> saveHead(UserInfo user, HttpServletRequest request){
        HashMap<String,Object> map = new HashMap<>();
        String info = userInfoService.updateHead(user,request);
        map.put("info",info);
        return map;

    }

}
